import React from 'react';
import ListReport from './ReportTable';
import Dept1Analysis from './dept1Analysis';
import Dept2Analysis from './dept2Analysis';
import EmplAnalysis from './emplAnalysis';
import CompClieAnalysis from './compClieAnalysis';
import IndiClieAnalysis from './indiClieAnalysis';
import { Drawer, message } from 'antd';
// 属性类型
type PropField = {
  drillDown: API.ReportDrillDown;
  columns: API.ReportColumn[];
  colName: string;
  whereAttach: string;
  titleColumns: string[];
  params: Record<string, any>;
  rowData: Record<string, any>;
  visible: boolean;
  hideDrillDownsDrawer: () => void;
  hasEmid: boolean;
};
const SubReport: React.FC<PropField> = (props) => {
  const { drillDown, columns, colName, titleColumns, params, rowData, hideDrillDownsDrawer } =
    props;
  if (!rowData || !props.visible || !drillDown) {
    return null;
  }
  let hasEmid = props.hasEmid;
  let whereAttach = props.whereAttach;
  const dd = drillDown;
  const titles: string[] = [];
  let colDesc: string = '';
  let colExp: string = '';
  let groupCalc: string = '';
  const getColName = (col: API.ReportColumn, pName: string) => {
    let cname = '';
    if (typeof col.title === 'string') {
      cname = col.title;
    }
    if (pName !== '') {
      cname = cname === '' ? pName : pName + '-' + cname;
    }
    if (col.dataIndex == colName) {
      colDesc = cname;
      colExp = col.colExp;
      if (colExp === '') {
        colExp = colName;
      }
      groupCalc = col.groupCalc;
    } else {
      if (col.children && col.children.length > 0) {
        for (let i = 0; i < col.children.length; i++) {
          getColName(col.children[i], cname);
        }
      }
    }
  };
  if (colName) {
    for (let i = 0; i < columns.length; i++) {
      getColName(columns[i], '');
      if (colDesc) {
        break;
      }
    }
  }
  if (titleColumns && titleColumns.length > 0) {
    for (let i = 0; i < titleColumns.length; i++) {
      const cName = titleColumns[i];
      if (rowData[cName]) {
        titles.push(rowData[cName]);
      }
    }
    if (colDesc) {
      titles.push(colDesc);
    }
  } else {
    message.error(`没有为报表${dd.reptName}配置titleColumns`);
    return null;
  }
  const pvs: Record<string, any> = {};
  Object.keys(dd.params).map((k) => {
    const pv = dd.params[k];
    const prev = pv.substring(0, 1);
    const pName = pv.substring(1, pv.length);
    if (prev === '@') {
      pvs[k] = params[pName];
      if (pvs[k] === undefined) {
        message.error(`报表${dd.reptName}的参数名${pName}没有找到`);
        return;
      }
    } else if (prev === '$') {
      pvs[k] = rowData[pName];
      if (pvs[k] === undefined) {
        message.error(`报表${dd.reptName}的字段名${pName}没有找到`);
        return;
      }
    } else if (pv === '#COLEXP') {
      pvs[k] = colExp;
    } else if (pv === '#EMPL_LEVEL') {
      if (Number(rowData.EMID) >= 0) {
        pvs[k] = 'user';
      } else if (Number(rowData.DPID) >= 0) {
        pvs[k] = 'dept2';
      } else if (Number(rowData.PDID) >= 0) {
        pvs[k] = 'dept1';
      } else {
        pvs[k] = 'all';
      }
    } else if (pv === '#EMID') {
      hasEmid = true;
      if (Number(rowData.EMID) >= 0) {
        pvs[k] = rowData.EMID;
      } else if (Number(rowData.DPID) >= 0) {
        pvs[k] = rowData.DPID;
      } else if (Number(rowData.PDID) >= 0) {
        pvs[k] = rowData.PDID;
      } else {
        pvs[k] = '0';
      }
    } else {
      pvs[k] = pv;
    }
  });
  const title = titles.join('-');
  let reptComp = null;
  pvs.groupCalc = groupCalc;
  if (dd.reptName === '#Dept1Analysis') {
    reptComp = (
      <Dept1Analysis
        sdate={pvs.sdate}
        edate={pvs.edate}
        cyno={pvs.cyno}
        timeLevel={pvs.timeLevel}
        emplLevel={pvs.emplLevel}
        emplID={pvs.emplID}
        colName={pvs.colName}
        groupCalc={pvs.groupCalc}
      />
    );
  } else if (dd.reptName === '#Dept2Analysis') {
    reptComp = (
      <Dept2Analysis
        sdate={pvs.sdate}
        edate={pvs.edate}
        cyno={pvs.cyno}
        timeLevel={pvs.timeLevel}
        emplLevel={pvs.emplLevel}
        emplID={pvs.emplID}
        colName={pvs.colName}
        groupCalc={pvs.groupCalc}
      />
    );
  } else if (dd.reptName === '#EmplAnalysis') {
    reptComp = (
      <EmplAnalysis
        sdate={pvs.sdate}
        edate={pvs.edate}
        cyno={pvs.cyno}
        timeLevel={pvs.timeLevel}
        emplLevel={pvs.emplLevel}
        emplID={pvs.emplID}
        colName={pvs.colName}
        groupCalc={pvs.groupCalc}
      />
    );
  } else if (dd.reptName === '#CompClieAnalysis') {
    reptComp = (
      <CompClieAnalysis
        sdate={pvs.sdate}
        edate={pvs.edate}
        cyno={pvs.cyno}
        timeLevel={pvs.timeLevel}
        emplLevel={pvs.emplLevel}
        emplID={pvs.emplID}
        colName={pvs.colName}
        groupCalc={pvs.groupCalc}
      />
    );
  } else if (dd.reptName === '#IndiClieAnalysis') {
    reptComp = (
      <IndiClieAnalysis
        sdate={pvs.sdate}
        edate={pvs.edate}
        cyno={pvs.cyno}
        timeLevel={pvs.timeLevel}
        emplLevel={pvs.emplLevel}
        emplID={pvs.emplID}
        colName={pvs.colName}
        groupCalc={pvs.groupCalc}
      />
    );
  } else {
    let filter = '';
    if (hasEmid) {
      if (Number(rowData.EMID) >= 0) {
        filter = `EMID=${rowData.EMID}`;
      } else if (Number(rowData.DPID) >= 0) {
        filter = `toUInt64(EMID) IN (SELECT EMID FROM DICT_USER_DEPT WHERE DPID = ${rowData.DPID})`;
      } else if (Number(rowData.PDID) >= 0) {
        filter = `toUInt64(EMID) IN (SELECT EMID FROM DICT_USER_DEPT WHERE PDID = ${rowData.PDID})`;
      }
    }
    if (filter !== '') {
      if (whereAttach === '') {
        whereAttach = filter;
      } else {
        whereAttach = whereAttach + ' AND ' + filter;
      }
    }
    reptComp = (
      <ListReport
        reportName={dd.reptName}
        paramVals={pvs}
        whereAttach={whereAttach}
        height={400}
        canSelectItems={false}
        canClearFilter={false}
        canExport={true}
        hideParamBar={true}
        pageSize={20}
        refresh={true}
      />
    );
  }
  return (
    <Drawer
      title={title}
      placement={dd.drawer.placement as 'top' | 'right' | 'bottom' | 'left'}
      width={dd.drawer.width}
      height={dd.drawer.height}
      closable={true}
      onClose={hideDrillDownsDrawer}
      visible={props.visible}
      bodyStyle={{ paddingLeft: 10, paddingRight: 10, paddingTop: 0, paddingBottom: 0 }}
    >
      {reptComp}
    </Drawer>
  );
};

export default SubReport;
